import yaml
import os
from loguru import logger
from utils.object_utils import cache

# 返回项目路径，或者某个目录的路径
def get_path(dir,file_path=None):
    r"""
    返回结果 ：E:\python_project\api_test_49\data\user_add_flow.yml
    :param dir: 相当于项目下一级目录
    :param file_path: 目录下的文件
    :return:
    """
    project_path = os.path.dirname(os.path.dirname(__file__))
    path = os.path.join(project_path,dir)
    if file_path is not None:
        path = os.path.join(path,file_path)
        return path
    return path


# 读取yaml文件，返回python对象
def read_yaml_file(yml_file):
    yml_path = get_path('data',yml_file)
    with open(yml_path,encoding='utf-8') as yml_obj:
        data = yaml.safe_load(yml_obj)
        logger.info("返回测试用例数据为:{}".format(data))
        return data


# 判断传入的值是否包含{{ ，只要包含了{{ ，代表从缓存中提取 ，否则直接原值返回。
def is_contains_brace(value):
    """
    {{user_id}}
    返回的是真实的值
    :param value:
    :return:
    """
    start_index = value.find("{{")
    end_index = value.find("}}")
    if start_index >= 0 and end_index >= 0:
        vls = cache.get(value[start_index+2:end_index])
        logger.debug("从缓存中提取的键名:{},值:{}".format(value[start_index+2:end_index],vls))
        return vls
    return value



if __name__ == '__main__':
    # print(get_path('data','user_add_flow.yml'))
    print(read_yaml_file('user_add_flow.yml'))

